package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.MyException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public R saveSetMeal(SetmealDto setmealDto) {
        save(setmealDto);
        List<SetmealDish> flavors = setmealDto.getSetmealDishes();

        flavors.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        boolean b = setmealDishService.saveBatch(flavors);
        if (b) {
            return R.success("保存成功");
        }

        return R.error("保存失败");
    }

    @Override
    public R pageSetmeal(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo =new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(name),Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        page(pageInfo,lqw);
        List<Setmeal> records = pageInfo.getRecords();

        Set<Long> recordSet = records.stream().map(setmeal -> setmeal.getCategoryId()).collect(Collectors.toSet());
        List<Category> categories = categoryService.listByIds(recordSet);
        Map<Long, String> categoryMap = categories.stream().collect(Collectors.toMap(Category::getId, Category::getName));

        records.forEach(setmeal -> setmeal.setCategoryName(categoryMap.get(setmeal.getCategoryId())));


        return R.success(pageInfo);
    }

    @Override
    public R getSetMealById(Long id) {
        Setmeal setmeal = getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null!=id,SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    @Override
    public R updateSetMeal(SetmealDto setmealDto) {
        updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);

        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));

        boolean b = setmealDishService.saveBatch(setmealDishes);
        if (b) {
            return R.success("修改成功");
        }

        return R.error("修改失败");
    }

    @Override
    public R updateStatus(List<Setmeal> ids) {

        boolean b = updateBatchById(ids);
        if (b) {
            return R.success("修改成功");
        }

        return R.error("修改失败");
    }

    @Override
    public R deleteSetMeal(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper();
        lqw.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        int count = count(lqw);
        if (count>0) {
            throw new MyException("套餐正在售卖，删除失败");
        }
        removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lqw2);


        return R.success("删除成功");
    }
}
